package com.jiayuge.server.service.impl;

import com.jiayuge.server.common.entity.RespBean;
import com.jiayuge.server.pojo.Role;
import com.jiayuge.server.mapper.RoleMapper;
import com.jiayuge.server.service.IRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Jiayu.Yang
 * @since 2021-11-15
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {

    @Override
    public RespBean addRole(Role role) {
        //判断是否满足Security的权限命名规则  必须  ROLE_ 开头
        if (!role.getName().startsWith("ROLE_")){
            role.setName("ROLE"+role.getName());
        }

        //判断当前要插入的角色对象是否已存在
        List<String> roles = super.list().stream().map(role1 -> role1.getName()).collect(Collectors.toList());
        if (roles.contains(role.getName())) {
            return RespBean.error("添加失败，角色已存在");
        }
        if (super.save(role)){
            return RespBean.success("添加成功");
        }
        return RespBean.error("添加失败");
    }
}
